<template>
  <!-- 出库记录 -->
  <div>
    <el-card class="box-card" :body-style="{ padding: '10px 10px 2px 10px' }">
      <el-form :inline="true" :model="queryParam" class="demo-form-inline" size="medium">
        <el-form-item label="单号">
          <el-input v-model="queryParam.billNumber" placeholder="请输入单号" clearable></el-input>
        </el-form-item>
        <el-form-item label="出库日期">
          <el-date-picker v-model="upDate" type="daterange" range-separator="至" clearable start-placeholder="开始日期"
            end-placeholder="结束日期" @change="handleDateChange">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出库类型">
          <el-select v-model="queryParam.labelType" placeholder="请选择出库类型" class="full-width-input" clearable>
            <el-option v-for="(item, index) in labelTypeOptions" :key="index" :label="item.label" :value="item.value"
              :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出库状态">
          <el-select v-model="queryParam.outStatus" placeholder="请选择出库类型" class="full-width-input" clearable>
            <el-option v-for="(item, index) in outStatusOptions" :key="index" :label="item.label" :value="item.value"
              :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-tooltip content="来自第三方的数据" placement="top" effect="light">
            <el-button type="primary" @click="confirmOut">出库确认</el-button>
          </el-tooltip>
        </el-form-item>
      </el-form>
      <el-row :gutter="24">
        <el-col :span="24"> </el-col>
      </el-row>
    </el-card>

    <el-card class="box-card" :body-style="{ padding: 10 }">
      <el-table :data="tableData" border style="width: 100%" height="50vh" ref="table"
        @selection-change="selectionChange">
        <el-table-column type="selection" width="55" align="center" sortable> </el-table-column>
        <el-table-column v-for="item of tableProps" :key="item.props" :prop="item.prop" :label="item.title"
          :width="item.width">
          <template #default="scope">
            <div v-if="item.prop === 'labelType'">
              <el-tag v-show="scope.row.labelType === 0" type="success">档案出库</el-tag>
              <el-tag v-show="scope.row.labelType === 1">盒出库</el-tag>
            </div>
            <div v-else-if="item.prop === 'dataSources'">
              <span v-show="scope.row.dataSources === 0">平台端</span>
              <span v-show="scope.row.dataSources === 1">终端设备</span>
              <span v-show="scope.row.dataSources === 2">数据接口</span>
            </div>
            <div v-else-if="item.prop === 'outStatus'">
              <el-tag v-show="scope.row.outStatus === 0">未出库</el-tag>
              <el-tag v-show="scope.row.outStatus === 1" type="success">已出库</el-tag>
              <el-tag v-show="scope.row.outStatus === 2" type="warning">拒绝出库</el-tag>
            </div>
            <div v-else>{{ scope.row[item.prop] }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="{ row }">
            <el-button icon="el-icon-view" link size="mini" @click="handleShow(row)">查看</el-button>
            <el-popconfirm v-if="row.outStatus !== 1" class="button10Left" title="确定要删除吗？"
              @confirm="() => handleDel(row)">
              <el-button slot="reference" icon="el-icon-delete" link size="mini" class="danger_button">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card class="box-card" :body-style="{ padding: 10 }">
      <pagination ref="pagination" :totalCount="totalCount" @pagination="pagination"></pagination>
    </el-card>
    <out-bill-dialog ref="outBillDialog" @ok="loadData"></out-bill-dialog>
  </div>
</template>

<script>
import { pageList, del } from '@/api/rfid/outBill'
import outBillDialog from './dialog/outBillDialog.vue'
import { resetDefaultArchiveFieldSort } from '@/api/sys/field'

import moment from 'moment'
export default {
  name: 'outBill',
  components: { outBillDialog },
  data() {
    return {
      totalCount: 0, //总数
      //查询条件
      searchModel: {
        pageNo: 1,
        pageSize: 10,
      },
      queryParam: {},
      upDate: [],
      tableData: [],
      tableProps: [
        { title: '出库单号', prop: 'billNumber', width: '', checked: true },
        { title: '出库时间', prop: 'outDate', width: '', checked: true },
        { title: '接收人', prop: 'recipient', width: '100', checked: true },
        { title: '出库类型', prop: 'labelType', width: '', checked: true },
        { title: '数据来源', prop: 'dataSources', width: '', checked: true },
        { title: '出库人', prop: 'outBy', width: '', checked: true },
        { title: '出库状态', prop: 'outStatus', width: '100', checked: true },
        { title: '制单时间', prop: 'createTime', width: '', checked: true },
      ],
      labelTypeOptions: [
        {
          label: '档案出库',
          value: 0,
        },
        {
          label: '盒出库',
          value: 1,
        },
      ],
      outStatusOptions: [
        {
          label: '未出库',
          value: 0,
        },
        {
          label: '已出库',
          value: 1,
        },
        {
          label: '拒绝出库',
          value: 2,
        },
      ],
      selectionData: [],
    }
  },

  mounted() {
    this.loadData()
  },

  methods: {
    handleDateChange(value) {
      if (value === null) {
        this.queryParam.beginTime = null
        this.queryParam.endTime = null
      } else {
        this.queryParam.beginTime = moment(value[0]).format('YYYY-MM-DD 00:00:00')
        this.queryParam.endTime = moment(value[1]).format('YYYY-MM-DD 23:59:59')
      }
    },
    onSubmit() {
      this.$refs.pagination.resetPagination()
    },
    reset() {
      this.upDate = []
      this.queryParam = {}
      this.$refs.pagination.resetPagination()
    },
    confirmOut() {
      if (this.selectionData.length === 0) {
        this.$message.warning('请选择要确认的出库单')
        return
      }
      if (this.selectionData.length > 1) {
        this.$message.warning('只能选择一条出库单')
        return
      }
      if (this.selectionData[0].outStatus !== 0) {
        this.$message.warning('只能选择未出库的出库单')
        return
      }
      this.$refs.outBillDialog.confirm(this.selectionData[0])
    },
    selectionChange(val) {
      this.selectionData = val
    },
    loadData() {
      this.tableData = []
      var param = Object.assign(this.queryParam, this.searchModel)
      pageList(param)
        .then((res) => {
          if (res.data.code === 200) {
            this.tableData = res.data.data.records
            this.totalCount = res.data.data.total
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    pagination(val) {
      this.searchModel.pageNo = val.page
      this.searchModel.pageSize = val.limit
      this.loadData()
    },
    async UpdateTable(tableProps) {

      const AfterTable = tableProps.map((item, index) => {
        return {
          defineId: item.defineId,
          title: item.title,
          fieldSort: index,
        }
      })
      const { data } = await resetDefaultArchiveFieldSort(AfterTable)
      if (data.code == 200) {
        this.tableProps = this.onUpdateTable(tableProps)
      } else {
        this.$message.error(data.message)
      }
    },
    handleShow(row) {
      this.$refs.outBillDialog.show(row)
    },
    handleDel(row) {
      del(row.billNumber)
        .then((res) => {
          if (res.data.code === 200) {
            this.$message.success(res.data.message)
            this.loadVerData()
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
  },
}
</script>

<style lang="less" scoped>
.el-form-item {
  margin-bottom: 8px;
}
</style>
